package com.xc.hotelmgr.order.modules.ali.controller;
import com.xc.hotelmgr.order.common.utils.Constant;
import com.xc.hotelmgr.order.common.utils.R;
import com.xc.hotelmgr.order.modules.ali.service.DeliGoodsService;
//import com.xc.hotelmgr.order.modules.ali.service.OrderFeignService;
import com.xc.hotelmgr.order.modules.ali.service.ReceOrdeService;
import com.xc.hotelmgr.order.modules.ali.to.bookrq.request.BookRQ;
import com.xc.hotelmgr.order.modules.ali.to.deligood.DeliGood;
import com.xc.hotelmgr.order.modules.ali.to.manuorder.ManuBookRQ;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 飞猪订单接入
 *
 * @author kemmy
 */
@Slf4j
@RestController
public class OrderController {
    @Autowired
    private ReceOrdeService orderService;

    @Autowired
    private DeliGoodsService deliGoodsService;

//    @Autowired
//    private OrderFeignService orderFeignService;

    @RequestMapping("order")
    public void aliOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, DocumentException, InterruptedException {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null)
        {
            sb.append(line);
        }
        String postXMLData = sb.toString();
        log.warn("==postXMLData:{}",postXMLData);
        System.out.println(postXMLData);
        String result = orderService.router(postXMLData);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(result);
    }

    /**
     * 前端调用下单
     * @param manuBookRQ
     * @return
     */
    @RequestMapping("manu/order")
    public R manuOrder(@RequestBody ManuBookRQ manuBookRQ) throws InterruptedException {
        log.info("==manuBookRQ==",manuBookRQ);
        return orderService.manuBook(manuBookRQ);
    }

    /**
     * 前端调用关单
     * @param deliGood
     * @return
     */
    @RequestMapping("manu/deligood")
    public R deliGood(@RequestBody DeliGood deliGood){
        return deliGoodsService.deliGood(deliGood.getOrderCode(), Constant.OptType.getOptTypeByValue(deliGood.getOptType()),"");
    }

    /**
     * 前端调用openfeign测试
     * @return
     */
    @RequestMapping("manu/test")
    public R test(){
        return R.ok();
    }

//    /**
//     * 前端调用openfeign测试
//     * @return
//     */
//    @RequestMapping("manu/test2")
//    public R test2(){
//        return orderFeignService.test();
//    }
}
